Usage Sample

The interface is kept simple: you feed in HAML strings and get back a string of PHP code you can eval. For convenience, a ready-to-use interface caches PHP code in files.

require_once(dirname(__FILE__).'/Haml.php');
define('BASE', dirname(__FILE__));
define('HAML_DIR', BASE.'/haml');
define('HAML_TEMPLATE_CACHE', BASE.'/template-cache');

// Create a caching HAML object
$haml = new HamlFileCache(HAML_DIR, HAML_TEMPLATE_CACHE);

// Update cache on each request (defaults to false)
$haml->forceUpdate = true;

// Somewhat prettier HTML (defaults to true)
$haml->options['ugly'] = false;

// xdebug prevents segfaults caused by stack overflows
// Default is 100 which is not enough for HAML-TO-PHP
@ini_set('xdebug.max_nesting_level', '600');

// Data passed to the .haml file; extract() puts keys in scope
$data = array('title' => "OH HAPPY DAY");

// Run the template. Parsing, translating to PHP, and writing
// the cache file are done automatically.
echo $haml->haml('test.haml', $data);
What is HAML-TO-PHP